﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SeDuce.BLL;
using Ajax;
using SeDuce.Model;
using System.Data.SqlClient;
using SeDuce.DAL;

public partial class Search : RepeaterPagerBase
{
    Lazy_Yu<BrandBLL> lazyBrandBLL = new Lazy_Yu<BrandBLL>(() => new BrandBLL());
    public BrandBLL LazyBrandBLL
    {
        get { return this.lazyBrandBLL.Value; }
    }
    
    Lazy_Yu<AdBLL> lazyAdBLL = new Lazy_Yu<AdBLL>(() => new AdBLL());
    public AdBLL LazyAdBLL
    {
        get { return this.lazyAdBLL.Value; }
    }

    Lazy_Yu<ProductBLL> lazyProductBLL = new Lazy_Yu<ProductBLL>(() => new ProductBLL());
    public ProductBLL LazyProductBLL
    {
        get { return this.lazyProductBLL.Value; }
    }

    public SqlParameter[] SP
    {
        get
        {
            return new SqlParameter[] { 
                new SqlParameter("@SearchString", this.GetQueryString("keywords",true))
            };
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(Search));
        BindCategories();
        if (!IsPostBack)
        {
            this.rptAds.DataSource = this.LazyAdBLL.GetAds().ToList<Ad>().Where<Ad>(a => a.Enabled == true);
            this.rptAds.DataBind();
            this.rptNewProducts.DataSource = this.LazyProductBLL.GetProductsTop(15).ToList<SeDuce.Model.Product>().OrderByDescending(p => p.ProductID);
            this.rptNewProducts.DataBind();
        }
        this.RegistrPager(this.rptProducts, this.rptPageFliper, 10, this.FillPageMoudleWithParams(PageSize, string.Format(SeDuce.DAL.ProductDAL.sqlSearchProducts, "@SearchString"), this.SP, new List<SortField>() { 
            new SortField() { FieldName = "Seq", DESC = true },
            new SortField() { FieldName = "Date", DESC = true }
        }), this.panelEmpty);
    }

    protected void rptCategories_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "CATEGORY_CHANGE")
        {
            LinkButton lbtnCategory = (LinkButton)e.Item.FindControl("lbtnCategory");
            Response.Redirect(ResolveUrl("~/Brands/" + e.CommandArgument.ToString()));
        }
    }

    private void BindCategories()
    {
        this.rptCategories.DataSource = this.LazyBrandBLL.GetBrands().ToList<Brand>().OrderBy(t => t.Sort).ToList<Brand>();
        this.rptCategories.DataBind();
    }

    [Ajax.AjaxMethod()]
    public bool UpdateBrands()
    {
        try
        {
            BindCategories();
            return true;
        }
        catch { return false; }
    }

    protected string GetProductStatus(int statusID)
    {
        string status = string.Empty;
        switch (statusID)
        {
            case 1:
                status = ResolveUrl("~/images/status_spot.gif");
                break;
            case 2:
                status = ResolveUrl("~/images/status_reservation.gif");
                break;
            case 3:
                status = ResolveUrl("~/images/status_sale.gif");
                break;
            case 4:
                status = ResolveUrl("~/images/status_soldout.gif");
                break;
            default:
                break;
        }
        return status;
    }
}